home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue46 / Alfresco / AAChStk.pas next >
Encoding:
Pascal/Delphi Source File  |  1999-04-17  |  3.2 KB  |  122 lines

  1. {*********************************************************}
  2. {* AAChStk                                               *}
  3. {* Copyright (c) Julian M Bucknall 1998-1999             *}
  4. {* All rights reserved.                                  *}
  5. {*********************************************************}
  6. {* Character stack                                       *}
  7. {*********************************************************}
  8.  
  9. {Note: this unit is released as freeware. In other words, you are free
  10.        to use this unit in your own applications, however I retain all
  11.        copyright to the code. JMB}
  12.  
  13. unit AAChStk;
  14.  
  15. interface
  16.  
  17. uses
  18.   SysUtils;
  19.  
  20. type
  21.   TaaCharacterStack = class
  22.     private
  23.       FStack : PChar;
  24.       FSP    : integer;
  25.       FSize  : integer;
  26.     protected
  27.     public
  28.       constructor Create;
  29.         {-create the character stack}
  30.       destructor Destroy; override;
  31.         {-destroy the character stack; releasing all memory}
  32.  
  33.       procedure Clear;
  34.         {-remove all characters from stack}
  35.       function Count : integer;
  36.         {-count of characters on the stack}
  37.       function Examine : char;
  38.         {-return the top character from the stack; don't pop it}
  39.       function IsEmpty : boolean;
  40.         {-is the stack empty?}
  41.       function Pop : char;
  42.         {-pop the top character from the stack; return it}
  43.       procedure Push(aCh : char);
  44.         {-push the given character onto the stack}
  45.     end;
  46.  
  47. implementation
  48.  
  49. {===TaaCharacterStack===================================================}
  50. constructor TaaCharacterStack.Create;
  51. begin
  52.   inherited Create;
  53.   FSize := 1024;
  54.   FStack := StrAlloc(FSize);
  55.   FSP := -1;
  56. end;
  57. {--------}
  58. destructor TaaCharacterStack.Destroy;
  59. begin
  60.   if (FStack <> nil) then 
  61.     StrDispose(FStack);
  62.   inherited Destroy;
  63. end;
  64. {--------}
  65. procedure TaaCharacterStack.Clear;
  66. begin
  67.   FSP := -1;
  68. end;
  69. {--------}
  70. function TaaCharacterStack.Count : integer;
  71. begin
  72.   Result := succ(FSP);
  73. end;
  74. {--------}
  75. function TaaCharacterStack.Examine : char;
  76. begin
  77.   {check for the obvious mistake}
  78.   if (FSP = -1) then
  79.     raise Exception.Create('TaaCharacterStack.Examine: the stack is empty');
  80.   {return the current character}
  81.   Result := FStack[FSP];
  82. end;
  83. {--------}
  84. function TaaCharacterStack.IsEmpty : boolean;
  85. begin
  86.   Result := (FSP = -1);
  87. end;
  88. {--------}
  89. function TaaCharacterStack.Pop : char;
  90. begin
  91.   {check for the obvious mistake}
  92.   if (FSP = -1) then
  93.     raise Exception.Create('TaaCharacterStack.Pop: the stack is empty');
  94.   {return the current character}
  95.   Result := FStack[FSP];
  96.   {decrement the stack pointer}
  97.   dec(FSP);
  98. end;
  99. {--------}
  100. procedure TaaCharacterStack.Push(aCh : char);
  101. var
  102.   NewSize  : integer;
  103.   NewStack : PChar;
  104. begin
  105.   {increment the stack pointer}
  106.   inc(FSP);
  107.   {if we've run out of space, reallocate the stack buffer}
  108.   if (FSP = FSize) then begin
  109.     NewSize := FSize + 256;
  110.     NewStack := StrAlloc(NewSize);
  111.     Move(FStack^, NewStack^, FSize);
  112.     StrDispose(FStack);
  113.     FStack := NewStack;
  114.     FSize := NewSize;
  115.   end;
  116.   {store the pushed character}
  117.   FStack[FSP] := aCh;
  118. end;
  119. {====================================================================}
  120.  
  121. end.
  122.